package com.training.java.redbox.struts.domain;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class OrdersDAOJDBCImpl extends BaseDAO implements OrdersDAO {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public long getOrderNumber() throws DAOException {
		return jdbcTemplate.queryForObject(
				"select order_number_seq.nextval from dual", Long.class );
		
	}

	@Override
	public void createOrders(Orders orders) throws DAOException {

		jdbcTemplate.update(
				"insert into orders values(order_id_seq.nextval,?,?,?,?)",
				orders.getCustomer_id(), orders.getMovie_id(),
				orders.getStatus(), orders.getOrderNumber());

	}

	
/*	public static final class orderRowMapper implements RowMapper<Long> {
		@Override
		public Long mapRow(ResultSet resultSet, int rowNum) throws SQLException {

			System.out.println("rownum=" + rowNum);
			long orderNumber = resultSet.getLong(1);
			System.out.println("order number=" + orderNumber);
			return orderNumber;
		}

	}
*/
	@Override
	public List<Orders> getAllOrders() throws DAOException {
		return null;
	}

	@Override
	public Orders getOrdersById(long id) throws DAOException {
		return null;
	}

	@Override
	public void update(Orders orders) throws DAOException {

	}

	@Override
	public void delete(long id) throws DAOException {

	}

	/*
	 * @Override public List<Orders> getAllOrders() throws DAOException {
	 * 
	 * List<Orders> order = new ArrayList<Orders>();
	 * 
	 * Connection con = null; Statement stmt = null; ResultSet rs = null;
	 * 
	 * try { con = getConnection(); stmt = con.createStatement(); rs =
	 * stmt.executeQuery("select * from Orders");
	 * 
	 * while (rs.next()) { long id = rs.getLong("order_id"); long order_number=
	 * rs.getLong("order_number"); long customer_id = rs.getLong("customer_id");
	 * long movie_id = rs.getLong("movie_id"); String
	 * status=rs.getString("status"); long rental_days =
	 * rs.getLong("rental_days");
	 * 
	 * 
	 * Orders orders = new Orders(id, customer_id, movie_id,
	 * rental_days,order_number); orders.setStatus(status); order.add(orders); }
	 * return order;
	 * 
	 * } catch (SQLException e) { throw new DAOException(e); } finally {
	 * closeResources(con, stmt, rs); }
	 * 
	 * }
	 * 
	 * 
	 * @Override public Orders getOrdersById(long id) throws DAOException {
	 * Connection con = null; PreparedStatement stmt = null; ResultSet rs =
	 * null;
	 * 
	 * Orders order = null; try { con = getConnection(); stmt =
	 * con.prepareStatement("select * from order where id = ?"); stmt.setLong(1,
	 * id); rs = stmt.executeQuery();
	 * 
	 * if (rs.next()) { long Id = rs.getLong("id"); long customer_id =
	 * rs.getLong("customer_id"); long movie_id = rs.getLong("movie_id"); long
	 * rental_days = rs.getLong("rental_days"); long order_number=
	 * rs.getLong("order_number");
	 * 
	 * order = new Orders(id, customer_id, movie_id, rental_days, order_number);
	 * } return order;
	 * 
	 * } catch (SQLException e) { throw new DAOException(e); } finally {
	 * closeResources(con, stmt, rs); } }
	 * 
	 * @Override public void update(Orders orders) throws DAOException { {
	 * Connection con = null; PreparedStatement stmt = null;
	 * 
	 * try { con = getConnection(); stmt = con .prepareStatement(
	 * "update orders set id = ?, customer_id= ?, movie_id = ?, rental_days = ? where id = ?"
	 * ); stmt.setLong(1, orders.getId()); stmt.setLong(2,
	 * orders.getCustomer_id()); stmt.setLong(3, orders.getMovie_id());
	 * stmt.setLong(4, orders.getRental_days());
	 * 
	 * stmt.execute();
	 * 
	 * } catch (SQLException e) { throw new DAOException(e); } finally {
	 * closeResources(con, stmt, null); } }
	 * 
	 * }
	 * 
	 * @Override public void delete(long id) throws DAOException { Connection
	 * con = null; PreparedStatement stmt = null;
	 * 
	 * try { con = getConnection(); stmt =
	 * con.prepareStatement("delete from orders where id = ?"); stmt.setLong(1,
	 * id); stmt.execute();
	 * 
	 * } catch (SQLException e) { throw new DAOException(e); } finally {
	 * closeResources(con, stmt, null); }
	 * 
	 * }
	 */
}
